package com.hm.controller;

import com.hm.utils.FastDFSClient;
import com.hm.utils.HmResult;
import com.hm.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@RestController
@Api(value = "头像上传",tags = "头像上传")
public class PictureController {

    /*@Value("${IMAGE_SERVER_URL}")
    private String IMAGE_SERVER_URL;*/

    @ApiOperation(value = "头像上传")
    @RequestMapping(value = "/pic/upload",method = RequestMethod.POST)
    public HmResult fileUpload(@RequestParam("uploadFile") MultipartFile uploadFile) {
        System.out.println(uploadFile);
        try {
            //1、取文件的扩展名
            String originalFilename = uploadFile.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            //2、创建一个FastDFS的客户端
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/client.conf");
            //3、执行上传处理
            String path = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
            //4、拼接返回的url和ip地址，拼装成完整的url
            String url = "http://120.79.162.68/" + path;
            return HmResult.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            return HmResult.build(500, "图片上传失败");
        }
    }
}
